﻿using SuporeSliplampApp.Models;
using SuporeSliplampApp.Properties;
using SuporeSliplampApp.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

namespace SuporeSliplampApp.Views
{
    /// <summary>
    /// CameraSettingWindow.xaml 的交互逻辑
    /// </summary>
    public partial class CameraSettingWindow : Window
    {
        RGBModel rgbModel = new RGBModel();
        private int red = 0, green = 0, blue = 0;

        public CameraSettingWindow()
        {
            InitializeComponent();

            initData();

            red = Common.red;
            green = Common.green;
            blue = Common.blue;

            CameraView.cam_?.get(uvcham.eCMD.WBRED, out red);
            CameraView.cam_?.get(uvcham.eCMD.WBGREEN, out green);
            CameraView.cam_?.get(uvcham.eCMD.WBBLUE, out blue);

            r_slider.Value = red;
            g_slider.Value = green;
            b_slider.Value = blue;

            rgbModel.redvalue = red;
            rgbModel.greenvalue = green;
            rgbModel.bluevalue = blue;

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void expoSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if ((CameraView.cam_ != null) && CommonData.started_)
            {
                CameraView.cam_.put(uvcham.eCMD.EXPOTIME, (int)expoSlider.Value);
                r_txt.Text = ((int)expoSlider.Value).ToString();
            }
        }

        private void r_slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if ((CameraView.cam_ != null) && CommonData.started_)
            {
                CameraView.cam_.put(uvcham.eCMD.WBRED, (int)r_slider.Value);
                r_txt.Text = ((int)r_slider.Value).ToString();

                rgbModel.redvalue = (int)r_slider.Value;
                //string json = Newtonsoft.Json.JsonConvert.SerializeObject(rgbModel, Newtonsoft.Json.Formatting.Indented);
                //File.WriteAllText("RGBvalue.json", json);
            }
        }

        private void g_slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if ((CameraView.cam_ != null) && CommonData.started_)
            {
                CameraView.cam_.put(uvcham.eCMD.WBGREEN, (int)g_slider.Value);
                g_txt.Text = ((int)g_slider.Value).ToString();

                rgbModel.greenvalue = (int)g_slider.Value;
                //string json = Newtonsoft.Json.JsonConvert.SerializeObject(rgbModel, Newtonsoft.Json.Formatting.Indented);
               //File.WriteAllText("RGBvalue.json", json);
            }
        }

        private void b_slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if ((CameraView.cam_ != null) && CommonData.started_)
            {
                CameraView.cam_.put(uvcham.eCMD.WBBLUE, (int)b_slider.Value);
                b_txt.Text = ((int)b_slider.Value).ToString();

                rgbModel.bluevalue = (int)b_slider.Value;
               // string json = Newtonsoft.Json.JsonConvert.SerializeObject(rgbModel, Newtonsoft.Json.Formatting.Indented);
                //File.WriteAllText("RGBvalue.json", json);
            }
        }

        private void initData()
        {
            //auto_exposure.IsEnabled = CommonData.Expo_enabled;
            white_balance.IsEnabled = CommonData.balance_enabled;
            expoSlider.IsEnabled = CommonData.Expo_enabled;
            r_slider.IsEnabled = CommonData.R_enabled;
            g_slider.IsEnabled = CommonData.G_enabled;
            b_slider.IsEnabled = CommonData.B_enabled;
            r_slider.IsEnabled = true;
            g_slider.IsEnabled = true;
            b_slider.IsEnabled = true;
            auto_exposure.IsChecked = CommonData.Expo_checked;
            //auto_exposure.IsChecked = true;
            white_balance.IsChecked = CommonData.balance_checked;
            expoSlider.Maximum = CommonData.ExpoSlider_Max;
            expoSlider.Minimum = CommonData.ExpoSlider_Min;
            //r_slider.Maximum = CommonData.R_Max;
            r_slider.Minimum = CommonData.R_Min;
            //g_slider.Maximum = CommonData.R_Max;
            g_slider.Minimum = CommonData.R_Min;
            //b_slider.Maximum = CommonData.B_Max;
            b_slider.Minimum = CommonData.B_Min;

            if (Common.isexpo == true)
            {
                auto_exposure.IsChecked = true;
                expoSlider.IsEnabled = false;
            }
            if (Common.isexpo == false)
            {
                auto_exposure.IsChecked = false;
                expoSlider.IsEnabled = true;
            }

            if (Common.iswhite == true)
            {
                white_balance.IsChecked = true;
                r_slider.IsEnabled = false;
                g_slider.IsEnabled = false;
                b_slider.IsEnabled = false;
                CameraView.cam_?.put(uvcham.eCMD.WBMODE, 1);
            }
            if (Common.iswhite == false)
            {
                white_balance.IsChecked = false;
                r_slider.IsEnabled = true;
                g_slider.IsEnabled = true;
                b_slider.IsEnabled = true;
                CameraView.cam_?.put(uvcham.eCMD.WBMODE, 0);
            }

        }

        private void white_balance_Click(object sender, RoutedEventArgs e)
        {
            int temp = 0;
            if (CommonData.started_)
            {
                //CameraView.cam_?.put(uvcham.eCMD.WBMODE, (white_balance.IsChecked ?? false) ? 1 : 0);
                if (white_balance.IsChecked == false)
                {
                    temp = 0;
                    r_slider.IsEnabled = true;
                    g_slider.IsEnabled = true;
                    b_slider.IsEnabled = true;

                    CameraView.cam_?.put(uvcham.eCMD.WBMODE, temp);

                    Common.iswhite = false;

                    CameraView.cam_?.put(uvcham.eCMD.WBRED, red);
                    CameraView.cam_?.put(uvcham.eCMD.WBGREEN, green);
                    CameraView.cam_?.put(uvcham.eCMD.WBBLUE, blue);

                }
                else
                {
                    temp = 1;
                    r_slider.IsEnabled = false;
                    g_slider.IsEnabled = false;
                    b_slider.IsEnabled = false;

                    CameraView.cam_?.put(uvcham.eCMD.WBMODE, temp);
                    Common.iswhite = true;

                    Thread.Sleep(50);

                    CameraView.cam_.get(uvcham.eCMD.WBRED, out red);
                    CameraView.cam_.get(uvcham.eCMD.WBGREEN, out green);
                    CameraView.cam_.get(uvcham.eCMD.WBBLUE, out blue);

                    Thread.Sleep(50);
                    r_slider.Value = red;
                    g_slider.Value = green;
                    b_slider.Value = blue;

                }


                //r_slider.IsEnabled = !(white_balance.IsChecked ?? false);
                //g_slider.IsEnabled = !(white_balance.IsChecked ?? false);
                //b_slider.IsEnabled = !(white_balance.IsChecked ?? false);
            }

        }

        private void auto_exposure_Click(object sender, RoutedEventArgs e)
        {
            if (CommonData.started_)
            {
                CameraView.cam_?.put(uvcham.eCMD.AEXPO, (auto_exposure.IsChecked ?? false) ? 1 : 0);
                expoSlider.IsEnabled = !(auto_exposure.IsChecked ?? false);
                if (auto_exposure.IsChecked == true)
                {
                    Common.isexpo = true;
                }
                else
                {
                    Common.isexpo = false;
                }

            }
        }


        private void BtnSwitch_Click(object sender, RoutedEventArgs e)
        {
            if (CommonData.started_)
            {
                int ret = uvcham.switcFlg;
                CameraView.cam_?.put(uvcham.eCMD.USER, (uvcham.switcFlg == 1) ? 0 : 1);
                uvcham.switcFlg = (uvcham.switcFlg == 1 ? 0 : 1);

            }
        }

        private void LightSetting_Click(object sender, RoutedEventArgs e)
        {
            LightControlView lightControlView = new LightControlView();
            lightControlView.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
            lightControlView.ShowDialog();
        }


        private void Colorset_Click(object sender, RoutedEventArgs e)
        {
            //CameraView.cam_?.put(uvcham.eCMD.WBRED, 50);
            //CameraView.cam_?.put(uvcham.eCMD.WBGREEN, 68);
            //CameraView.cam_?.put(uvcham.eCMD.WBBLUE, 328);
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            try 
            {
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(rgbModel, Newtonsoft.Json.Formatting.Indented);
                File.WriteAllText("RGBvalue.json", json);
            }
            catch { }


        }

        //private void AFset_Click(Object sender, RoutedEventArgs e)
        //{
        //    CameraView.cam_?.put(uvcham.eCMD.AFMODE, 3);
        //    int feedbackStatus;
        //    CameraView.cam_.get(uvcham.eCMD.AFFEEDBACK, out feedbackStatus);

        //    if (feedbackStatus != 3)
        //    {
        //        if (Common.languageCode == "zh-CN")
        //        {
        //            idex.Text = "对焦校准完成！" + feedbackStatus.ToString();
        //        }
        //        else
        //        {
        //            idex.Text = "AF complete!" + feedbackStatus.ToString();
        //        }

        //    }
        //    //MessageBox.Show(feedbackStatus.ToString());
        //}

        private void Test_Click(object sender, RoutedEventArgs e)
        {
            int red , green , blue ;

            CameraView.cam_.get(uvcham.eCMD.WBRED,out red);
            CameraView.cam_.get(uvcham.eCMD.WBGREEN, out green);
            CameraView.cam_.get(uvcham.eCMD.WBBLUE, out blue);
            MessageBox.Show(red.ToString());
            MessageBox.Show(green.ToString());
            MessageBox.Show(blue.ToString());
        }
    }
}                         
